package com.hd.trans.files.model;

import android.app.Application;
import android.util.Pair;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;

import com.hd.trans.files.sort.ISort;


/**
 * Author: yangtianwen
 * Time: 2021/12/10
 * Des: 基础 ViewModel 继承 {@AndroidViewModel} 可以在 ViewModel 中使用 Context；
 * 实现 {@DefaultLifecycleObserver} 接口可在 ViewModel 中观察 UI 层的生命周期
 */
public abstract class BaseViewModel extends AndroidViewModel implements DefaultLifecycleObserver {

    // 排序方式
    private final MutableLiveData<ISort.SortMethod> mFilesSortObserver = new MediatorLiveData<>();
    // toast
    private final MutableLiveData<String> mToastObserver = new MutableLiveData<>();

    // 管理 Progress
    private ProgressUiViewModel mProgressUiViewModel;

    public BaseViewModel(@NonNull Application application) {
        super(application);
    }

    public void setProgressUiViewModel(ProgressUiViewModel progressUiViewModel) {
        this.mProgressUiViewModel = progressUiViewModel;
    }

    public ProgressUiViewModel getProgressUiViewModel() {
        return mProgressUiViewModel;
    }

    public MutableLiveData<String> getToastObserver() {
        return mToastObserver;
    }

    public MutableLiveData<ISort.SortMethod> getFilesSortObserver() {
        return mFilesSortObserver;
    }

    public static class ProgressUiViewModel {
        // 进度开始显示，一般设置 max progress
        private final MutableLiveData<Integer> mProgressStartObserver = new MutableLiveData<>();
        // 进度更新中， first：当前的进度值，second：最大的进度值
        private final MutableLiveData<Pair<Integer, Integer>> mProgressingObserver = new MutableLiveData<>();
        // 进度结束
        private final MutableLiveData<Boolean> mProgressEndObserver = new MutableLiveData<>();

        public MutableLiveData<Integer> getProgressStartObserver() {
            return mProgressStartObserver;
        }

        public MutableLiveData<Pair<Integer, Integer>> getProgressingObserver() {
            return mProgressingObserver;
        }

        public MutableLiveData<Boolean> getProgressEndObserver() {
            return mProgressEndObserver;
        }
    }
}
